#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "request.pb.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpServer.h"

#include <iostream>
#include <unordered_map>
#include <functional>
class Server
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<wangshu::TranslateRequest> TransRequestPtr;
    typedef std::shared_ptr<wangshu::AddRequest> AddRequestPtr;
    typedef std::shared_ptr<wangshu::AddResponse> AddResponsePtr;

    Server(int port) : _server(&_baseloop, 
        muduo::net::InetAddress("0.0.0.0", port), 
        "Server", muduo::net::TcpServer::Option::kReusePort),
        _dispatcher(std::bind(&Server::OnUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        // 注册业务请求处理函数
        _dispatcher.registerMessageCallback<wangshu::TranslateRequest>(std::bind(&Server::OnTranslate,
                                                        this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<wangshu::AddRequest>(std::bind(&Server::OnAdd,
                                                      this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        // 设置消息回调处理函数
        _server.setMessageCallback(std::bind(&ProtobufCodec::onMessage,
                                             &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        // 设置连接回调处理函数
        _server.setConnectionCallback(std::bind(&Server::Onconnection, this, std::placeholders::_1));
    }
    void Start()
    {
        _server.start();
        _baseloop.loop();
    }

private:
    std::string translate(const std::string &str)
    {
        static std::unordered_map<std::string, std::string> map = {
            {"hello", "你好"},
            {"string", "字符串"},
            {"wangshu", "大帅b"}};
        if (map.find(str) == map.end())
        {
            return "找不到捏";
        }
        return map[str];
    }
    void OnTranslate(const muduo::net::TcpConnectionPtr &conn, const TransRequestPtr &message, muduo::Timestamp)
    {
        // 提取有效信息
        std::string msg = message->msg();
        // 业务处理，进行翻译
        std::string resp_msg = translate(msg);
        // 组织protobuf响应
        wangshu::TranslateResponse resp;
        resp.set_msg(resp_msg);
        // 发送响应
        _codec.send(conn, resp);
    }
    void OnAdd(const muduo::net::TcpConnectionPtr &conn, const AddRequestPtr &message, muduo::Timestamp)
    {
        int num1 = message->num1();
        int num2 = message->num2();
        int result = num1 + num2;
        wangshu::AddResponse resp;
        resp.set_result(result);
        _codec.send(conn, resp);
    }
    void Onconnection(const ::muduo::net::TcpConnectionPtr &conn)
    {
        if (conn->connected())
        {
            LOG_INFO << "新连接建立成功";
        }
        else
        {
            LOG_INFO << "连接即将关闭";
        }
    }
    void OnUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "On Unknown Message" << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::net::EventLoop _baseloop;
    muduo::net::TcpServer _server;  // 服务器对象
    ProtobufDispatcher _dispatcher; // 请求分发器对象 ---注册请求处理函数
    ProtobufCodec _codec;           // protobuf协议处理对象 ---针对收到的数据进行protobuf协议处理
};

int main()
{
    Server svr(8088);
    svr.Start();
    return 0;
}